package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.StorageTypeStat;
import com.ruoyi.system.service.StorageTypeStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 存储类型占比统计 Controller
 *
 * @author 你的名字
 */
@RestController
@RequestMapping("/system/storageTypeStat")
public class StorageTypeStatController {
    @Autowired
    private StorageTypeStatService storageTypeStatService;

    /**
     * 查询存储类型占比列表
     */
    @GetMapping("/list")
    public AjaxResult list(StorageTypeStat storageTypeStat) {
        List<StorageTypeStat> list = storageTypeStatService.selectStorageTypeStatList(storageTypeStat);
        Set<String> names = new HashSet<>();
        Integer total = 0;
        if(list != null && list.size() > 0)
        {
            names = list.stream().map(m -> m.getName()).collect(Collectors.toSet());
            total = list.stream().mapToInt(StorageTypeStat::getValue).sum();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("seriesData",list);
        map.put("xData",names);
        map.put("total",total);
        return AjaxResult.success(map); // 封装分页结果

    }

    /**
     * 获取存储类型占比详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(storageTypeStatService.selectStorageTypeStatById(id));
    }

    /**
     * 新增存储类型占比
     */
    @PostMapping
    public AjaxResult add(@RequestBody StorageTypeStat storageTypeStat) {
        return AjaxResult.success(storageTypeStatService.insertStorageTypeStat(storageTypeStat));
    }

    /**
     * 修改存储类型占比
     */
    @PutMapping
    public AjaxResult edit(@RequestBody StorageTypeStat storageTypeStat) {
        return AjaxResult.success(storageTypeStatService.updateStorageTypeStat(storageTypeStat));
    }

    /**
     * 删除存储类型占比
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(storageTypeStatService.deleteStorageTypeStatByIds(ids));
    }

    /**
     * 批量保存存储类型占比（先删后插）
     */
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody List<StorageTypeStat> list) {
        return AjaxResult.success(storageTypeStatService.batchSaveStorageTypeStat(list));
    }
}